package formulario;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JButton;

import Objetos.Libros;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.zip.GZIPOutputStream;

import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Font;
public class newLibro extends JFrame {

	private JPanel contentPane;
	public static JTextField txtTitulo;
	public static JTextField txtAutor;
	public static JTextField txtAno;
	public static JTextField txtIsbn;
	public static JTextField txtContenido;

	// Definimos una ArrayList
	public static List<Libros> list = new ArrayList<Libros>();
	public static Libros lib = null;
	public JLabel lblLibro;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					newLibro frame = new newLibro();
					frame.setResizable(false);
					frame.setTitle("Nuevo Libro");
					frame.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * Create the frame.
	 */
	public newLibro() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 521, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Titulo");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewLabel.setBounds(10, 69, 46, 14);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Autor");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewLabel_1.setBounds(10, 94, 46, 14);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblAoDeEdicin = new JLabel("A\u00F1o de edici\u00F3n");
		lblAoDeEdicin.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblAoDeEdicin.setBounds(10, 119, 95, 14);
		contentPane.add(lblAoDeEdicin);
		
		JLabel lblNewLabel_2 = new JLabel("ISBN");
		lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewLabel_2.setBounds(10, 142, 46, 14);
		contentPane.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Tabla de contenidos");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewLabel_3.setBounds(10, 173, 127, 14);
		contentPane.add(lblNewLabel_3);
		
		txtTitulo = new JTextField();
		txtTitulo.setBounds(147, 69, 173, 20);
		contentPane.add(txtTitulo);
		txtTitulo.setColumns(10);
		
		txtAutor = new JTextField();
		txtAutor.setBounds(147, 94, 173, 20);
		contentPane.add(txtAutor);
		txtAutor.setColumns(10);
		
		txtAno = new JTextField();
		txtAno.setBounds(147, 119, 86, 20);
		contentPane.add(txtAno);
		txtAno.setColumns(10);
		
		txtIsbn = new JTextField();
		txtIsbn.setBounds(147, 142, 86, 20);
		contentPane.add(txtIsbn);
		txtIsbn.setColumns(10);
		
		txtContenido = new JTextField();
		txtContenido.setBounds(147, 173, 173, 95);
		contentPane.add(txtContenido);
		txtContenido.setColumns(10);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 102, 255)));
		panel.setBounds(10, 11, 493, 40);
		contentPane.add(panel);
		
		JLabel lblNewLabel_4 = new JLabel("NUEVO LIBRO");
		lblNewLabel_4.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 19));
		panel.add(lblNewLabel_4);
		
		ImageIcon imagen = new ImageIcon("libros.jpg");
		JLabel etiqueta = new JLabel(imagen);
		JLabel lblNewLabel_5 = new JLabel(imagen);
		lblNewLabel_5.setBounds(330, 46, 145, 139);
		contentPane.add(lblNewLabel_5);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1.setBounds(330, 199, 173, 69);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JButton btnGuardar = new JButton("Guardar");
		btnGuardar.setBounds(10, 35, 108, 23);
		panel_1.add(btnGuardar);
		
		lblLibro = new JLabel("Libro #");
		lblLibro.setBounds(10, 10, 46, 14);
		panel_1.add(lblLibro);
		lblLibro.setForeground(Color.DARK_GRAY);
		lblLibro.setBackground(Color.GRAY);
		lblLibro.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 13));
		btnGuardar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//Obtenemos los datos del formulario
				String titulo = txtTitulo.getText();
				String autor = txtAutor.getText();
				int ano = Integer.parseInt(txtAno.getText());
				String isbn = txtIsbn.getText();
				String contenido = txtContenido.getText();
				//--------------------------------------------------------
				
				//Creamos el objeto Libro con los datos dentro
				Libros lib = new Libros(titulo,autor,ano,isbn,contenido);
				
			
				//Adicionamos a la lista
				list.add(lib);
				
				JOptionPane.showMessageDialog(null, "Registro guardado...");
				
				//---Colocamos los campos del formulario en limpio
				txtTitulo.setText("");
				txtAutor.setText("");
				txtAno.setText("");
				txtIsbn.setText("");
				txtContenido.setText("");
				//------------------------------
				
				Principal.cant = Principal.cant-1; //Disminuimos la variable cant, esta es la que almacena la cantidad de libros y esta en Principal
				lblLibro.setText(""+Principal.cant); //Actualizamos en label del formulario actual donde muestra la cantidad de lisbros que restan
				if (Principal.cant == 0) {
					//cerramos el formulario
					dispose();
				}
			
				
			}
		});
	}
}
